package com.changpay.zywy.ui.fragment.main

import android.content.Intent
import android.view.View
import com.example.havi.bean.HyChatUser
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.mvp.contract.MineContract
import com.changpay.zywy.bean.UserInfo
import com.changpay.zywy.mvp.presenter.MinePresenter
import com.changpay.zywy.ui.activity.loan.LoanFirstActivity
import com.changpay.zywy.ui.activity.machinecontrol.MachineControlActivity
import com.changpay.zywy.ui.activity.main.IncomeActivity
import com.changpay.zywy.ui.activity.mine.*
import com.changpay.zywy.ui.activity.partent.QuDaoActivity
import com.changpay.zywy.ui.activity.serivce.ServiceActivity
import com.changpay.zywy.utils.ImageLoader
import com.changpay.zywy.utils.Preference
import com.changpay.zywy.utils.StringUtil
import kotlinx.android.synthetic.main.fragment_mine.*
import kotlinx.android.synthetic.main.mine_head_layout.*

class MineFragment : BaseMvpFragment<MineContract.View, MineContract.Presenter>(), MineContract.View {

    private var head_image: String by Preference(Constant.HEAD_IMAGE, "")
    private var true_name: String by Preference(Constant.TRUE_NAME, "")
    private var sex: String by Preference(Constant.SEX, "")
    private var card_number: String by Preference(Constant.CARD_NUMBER, "")
    private var adress: String by Preference(Constant.ADRESS, "")
    private var city: String by Preference(Constant.CITY, "")
    private var age: String by Preference(Constant.AGE, "")
    private var member_mobile: String by Preference(Constant.MEMBER_MOBILE, "")
    private var approve: Int by Preference(Constant.IS_APPROVE, 0)
    private var chat_id: String by Preference(Constant.CHAT_ID, "")
    private var chat_password: String by Preference(Constant.CHAT_PASSWORD, "")


    private val onClickListener = View.OnClickListener { view ->
        if (approve == 0) {
            showToast("请先实名认证")
            startActivity(RenZhenActivity::class.java)
        } else {
            when (view.id) {
                R.id.mine_iv_editInfo -> startActivity(EditUserInfoActivity::class.java)
                R.id.mine_ll_one -> startActivity(IncomeActivity::class.java)
                R.id.mine_ll_two -> startActivity(MachineControlActivity::class.java)
                R.id.mine_ll_three -> startActivity(QuDaoActivity::class.java)
                R.id.mine_ll_four -> startActivity(ServiceActivity::class.java)
                R.id.mine_ll_five -> Intent(context, BankCardActivity::class.java).run {
                    putExtra("type", 0)
                    startActivity(this)
                }
                R.id.mine_ll_six -> startActivity(MessageActivity::class.java)
                R.id.mine_ll_seven -> startActivity(MyPolicyActivity::class.java)
                R.id.mine_ll_eight -> startActivity(AlreadyActivity::class.java)
                R.id.mine_ll_nine -> startActivity(LoanFirstActivity::class.java)
            }
        }

    }

    override fun showUserInfo(data: UserInfo) {
        ImageLoader.loadImage(mine_iv_head,data.member_avatar,5)
        mine_tv_name.text = data.truename
        mine_tv_phone.text = StringUtil.PhoneToPass(data.member_mobile)
        mine_tv_id.text =  data.member_name


        // 保存数据
        true_name = data.truename
        head_image = data.member_avatar
        sex = data.sex
        card_number = data.idcard
        adress = data.evolution_address
        city = data.evolution_city
        age = data.age
        member_mobile = data.member_mobile

        val chatUser = HyChatUser()
        chatUser.avatar = data.member_avatar
        chatUser.chatPassword = chat_password
        chatUser.chatUserName = data.truename
        chatUser.nickName = data.truename
        chatUser.chatId = chat_id
        chatUser.userId = data.member_id.toString()

    }

    companion object {
        fun getInstance(): MineFragment = MineFragment()
    }

    override fun createPresenter(): MineContract.Presenter = MinePresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_mine

    override fun lazyLoad() {
        mPresenter?.getUserInfo(token)
    }

    override fun initView(view: View) {
        super.initView(view)
        mine_iv_setting.setOnClickListener{
            startActivity(SettingActivity::class.java)
        }
        mine_iv_editInfo.setOnClickListener(onClickListener)
        mine_ll_one.setOnClickListener(onClickListener)
        mine_ll_two.setOnClickListener(onClickListener)
        mine_ll_three.setOnClickListener(onClickListener)
        mine_ll_four.setOnClickListener(onClickListener)
        mine_ll_five.setOnClickListener(onClickListener)
        mine_ll_six.setOnClickListener(onClickListener)
        mine_ll_seven.setOnClickListener(onClickListener)
        mine_ll_eight.setOnClickListener(onClickListener)
        mine_ll_nine.setOnClickListener(onClickListener)
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getUserInfo(token)
    }
}